package com.ukao.cashregister.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.SendModeAdapter;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.SendModeBean;
import com.ukao.cashregister.eventbus.PayEvent;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.widget.StateImageView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class SendModeDialogFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.choose_time_recycler)
    RecyclerView chooseTimeRecycler;
    @BindView(R.id.edit_name_cancel)
    Button editNameCancel;
    @BindView(R.id.edit_name_submit)
    Button editNameSubmit;
    Unbinder unbinder;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private SendModeAdapter mSendModeAdapter;
    private ArrayList<SendModeBean> mSendModeBean;
    private String val;


    public static SendModeDialogFragment newInstance(Serializable param1, String param2) {
        SendModeDialogFragment fragment = new SendModeDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSendModeBean=(ArrayList<SendModeBean> ) getArguments().getSerializable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_fragment_choose_time, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        initListener();
    }

    public void init() {
        title.setText("送件方式");
        chooseTimeRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mSendModeAdapter=new SendModeAdapter(activity,mSendModeBean);
        chooseTimeRecycler.setAdapter(mSendModeAdapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        mSendModeAdapter.setOnItemClickListener(mOnItemClickListener);
    }


    private OnItemClickListener mOnItemClickListener=new MyOnItemClickListener() {

        public SendModeBean currentLeverSelect;

        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            SendModeBean mitemLever = (SendModeBean) o;
            if (!mitemLever.isCheck()) {
                mitemLever.setCheck(!mitemLever.isCheck());
                List<SendModeBean> mData = mSendModeAdapter.getDatas();
                for (int j = 0; j < mData.size(); j++) {
                    if (i != j) {
                        mData.get(j).setCheck(false);

                    }
                }

                mSendModeAdapter.notifyDataSetChanged();
            }
        }
    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close_btn, R.id.edit_name_cancel, R.id.edit_name_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.edit_name_submit:
                SendModeBean   sendModeBeans=null;
                List<SendModeBean> mdata = mSendModeAdapter.getDatas();
                for (SendModeBean sendModeBean : mdata) {
                    if (sendModeBean.isCheck()){
                        sendModeBeans=sendModeBean;
                    }
                }
                PayEvent.postHasData(PayEvent.Message.sendMode,sendModeBeans);
                dismiss();

                break;
        }
    }
}
